$(document).ready(function(){
    $('.tips').tipsy({title:'obs',gravity: $.fn.tipsy.autoNS, delayIn:1000,html:true});
    $('.odd').tipsy({title:'obs',gravity: $.fn.tipsy.autoNS, delayIn:1000,html:true});
    $('.even').tipsy({title:'obs',gravity: $.fn.tipsy.autoNS, delayIn:1000,html:true});
    $("#anterior").click(function(){var pag = $("#pagina").val();if (pag>0){pag--;}$("#pagina").val(pag);$('form').submit();return false;});
    $("#proximo").click(function(){var pag = $("#pagina").val();pag++;$("#pagina").val(pag);$('form').submit();return false;});
    $("#filtro").change(function(){$("#pagina").val(0);});
    $("#chave").change(function(){$("#pagina").val(0);});

    $.ajaxSetup({
        cache: false,
        type: 'POST',
        dataType: 'json',
        error: function () {
            alert('Um erro de comunica��o com o servidor ocorreu!');
            $('#ajax-ldr').fadeOut('fast');
            $('#wait').fadeOut('fast');
        }
    });
    $('.estadotarefa').click(function(){
        var e = $(this);
        $('.messages').parent().remove();
        if (e.attr('checked')){
            $.ajax({
                url: "/tarefas/fechar",
                processData: false,
                data: 'id='+$(e).attr('nr'),
                beforeSend: function (){
                    var pos = $(e).position();
                    var width = $(e).width();
                    $("#ajax-ldr").css( {
                        "left": (pos.left+width+5) + "px",
                        "top":pos.top + "px"
                    } ).fadeIn('fast');
                    $("#wait").fadeIn('fast').html('Abrindo Tarefa...');
                },
                success: function(msg){
                    if (msg.status==false){
                        //$('.messages p').html('').append('<span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span><strong>Mensagem:</strong>'+msg.error);
                        $('<div>').attr('class','ui-widget')
                            .append(
                                $('<div>').attr('class','messages ui-state-highlight ui-corner-all').attr('style','margin-top: 20px; padding: 0pt 0.7em;')
                                    .append($('<p>')
                                        .append($('<span>').attr('class','ui-icon ui-icon-info'))
                                        .append('<strong>Mensagens: </strong>'+msg.error)
                                    )
                            ).insertAfter('.submit');
                        $('.messages').show();
                        $(e).attr('checked',false);
                    }
                    $('#ajax-ldr').fadeOut('fast');
                    $("#wait").fadeOut('fast');
                }
            });
        }else{
            $.ajax({
                url: "/tarefas/abrir",
                processData: false,
                data: 'id='+$(e).attr('nr'),
                beforeSend: function (){
                    var pos = $(e).position();
                    var width = $(e).width();
                    $("#ajax-ldr").css( {
                        "left": (pos.left+width+5) + "px",
                        "top":pos.top + "px"
                    } ).fadeIn('fast');
                    $("#wait").fadeIn('fast').html('Fechando Tarefa...');
                },
                success: function(msg){
                    if (msg.status==false){
                      //$('.messages p').html('').append('<span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span><strong>Mensagem:</strong>'+msg.error);
                        $('<div>').attr('class','ui-widget')
                            .append(
                                $('<div>').attr('class','messages ui-state-highlight ui-corner-all').attr('style','margin-top: 20px; padding: 0pt 0.7em;')
                                    .append($('<p>')
                                        .append($('<span>').attr('class','ui-icon ui-icon-info'))
                                        .append('<strong>Mensagens: </strong>'+msg.error)
                                    )
                            ).insertAfter('.submit');
                      $('.messages').show();
                      $(e).attr('checked',true);
                    }
                    $('#ajax-ldr').fadeOut('fast');
                    $("#wait").fadeOut('fast');
                }
            });
        }
    });

    $('.messages').fadeOut(4000);

});